﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Repository;
using Ext.Net;
using ExaminationSys.Helper;
using ExaminationSys.DataAccess.Entity;


namespace ExaminationSys.View.Paper
{
    public partial class PaperDetailAutoEdit : System.Web.UI.UserControl
    {
        private static readonly PaperDetailAutoService Proxy = ObjectFactory<PaperDetailAutoService>.Create();
        private static PaperDetailAuto entity = new PaperDetailAuto();
        private static readonly SectionInfoService ProxySectionInfo = ObjectFactory<SectionInfoService>.Create();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                entity = null;
                BindControl();
            }
        }

        public Store GridStore { get; set; }
        public string ID { get; set; }

        public string  PaperID { get;set;}
        public string PaperTitle { get;set;}

        public void BindControl()
        {
            var studySectionList = ProxySectionInfo.GetStudySectionList();
            foreach (var s in studySectionList)
            {
                Ext.Net.ListItem item = new Ext.Net.ListItem();         //每次创建一个Ext.Net.ListItem的对象
                item.Value = s.ID.ToString();
                item.Text = s.Name;
                SectionID.Items.Add(item);
            }

        }
                 
        public void SetData(string id,string paperId)
        {
            PaperDetailAutoEditPanel.Reset();
            lblPaperTitle.Text = PaperTitle;
            if (!string.IsNullOrEmpty(id))
            {
                var realId = new Guid(id);
                var PaperDetailAuto = Proxy.Get(realId);

                if (PaperDetailAuto != null)
                {
                    entity = PaperDetailAuto;
                    Level.SetValue(PaperDetailAuto.Level.ToString());
                    Degree.SetValue(PaperDetailAuto.Degree.ToString());
                    SectionKeyWrods.Value = PaperDetailAuto.SectionKeyWrods;
                    OrderIndex.Value = PaperDetailAuto.OrderIndex;
                    KeyWrods.Value = PaperDetailAuto.KeyWrods;
                    SectionID.Select(PaperDetailAuto.SectionID);
                    Title.Text = entity.Title;
                    ExerciseCount.Text = entity.ExerciseCount.ToString();

                    if (PaperDetailAuto.ID != null)
                    {
                        IDHidden.Value = PaperDetailAuto.ID.ToString();
                        X.Js.Call("setID", PaperDetailAuto.ID);
                    }
                }
            }
            else
            {
                entity = new PaperDetailAuto();
                PaperDetailAutoEditWindow.ClearContent();

            }

            X.Js.Call("setHidPaperID", paperId);




        }

        public void Show()
        {
            this.PaperDetailAutoEditWindow.Show();
        }



        protected void SavePaperDetailAuto(object sender, DirectEventArgs e)
        {
            string ID = IDHidden.Value.ToString();

            string title = Title.Text;
            if (string.IsNullOrEmpty(title))
            {
                MessageBoxHelper.ShowWarning("标题不能为空");
                return;
            }
            if (SectionID.SelectedItem == null || SectionID.SelectedItem.Value.ToString() == "")
            {
                MessageBoxHelper.ShowWarning("栏目不能为空");
                return;
            }
            string sectionID = SectionID.SelectedItem.Value.ToString();
            string sectionName = SectionID.Value.ToString();
            string paperNo = DateTime.Now.ToString("yyyyMMddhhmmss");

            if (Level.SelectedItem == null || Level.SelectedItem.Value.ToString() == "")
            {
                MessageBoxHelper.ShowWarning("阶段不能为空");
                return;
            }
            string level = Level.SelectedItem.Value.ToString();

            if (Degree.SelectedItem == null || Degree.SelectedItem.Value.ToString() == "")
            {
                MessageBoxHelper.ShowWarning("难度不能为空");
                return;
            }
            string degree = Degree.SelectedItem.Value.ToString();
 
 
            DateTime OpeatorTime = DateTime.Now;
            int status = 1;


            entity.SectionID = new Guid(SectionID.SelectedItem.Value);
            entity.PaperID = new Guid(HidPaperID.Value);
            entity.Title = title;


            entity.SectionKeyWrods = SectionKeyWrods.Text;

            int exerciseCount;
            if (ExerciseCount.Value == null || !int.TryParse(ExerciseCount.Value.ToString(), out exerciseCount))
            {
                MessageBoxHelper.ShowWarning("题目数请填写数字");
                return;
            };
            entity.ExerciseCount = exerciseCount;


            int orderindex;
            if (OrderIndex.Value == null || !int.TryParse(OrderIndex.Value.ToString(), out orderindex))
            {
                MessageBoxHelper.ShowWarning("排序请填写数字");
                return;
            };
            entity.OrderIndex = orderindex;
            entity.KeyWrods = KeyWrods.Text;
            entity.ExerciseType = 1;// ??


            int templevel;
            if (!int.TryParse(level, out templevel))
            {
                MessageBoxHelper.ShowWarning("阶段不能为空");
                return;
            }
            entity.Level = templevel;

            int tempDegree;
            if (!int.TryParse(degree, out tempDegree))
            {
                MessageBoxHelper.ShowWarning("难度不能为空");
                return;
            }
            entity.Degree = tempDegree;

           
            entity.Status = status;

            var op = FormsAuthenticationHelper.GetCurrentUserID();
            if (string.IsNullOrEmpty(op))
            {
                FormsAuthenticationHelper.SignOut();
                Response.Redirect("~/Account/Login.aspx");
            }
            entity.Operator = op;

            entity.OperatorTime = DateTime.Now;
            var sucess = false;
            // entity.
            if (string.IsNullOrWhiteSpace(IDHidden.Value))
            {
                entity.ID = Guid.NewGuid();
                var retCode = Proxy.Insert(entity);
                sucess = retCode != null;
            }
            else
            {
                sucess = Proxy.Update(entity);
            }

            if (sucess)
            {
                MessageBoxHelper.ShowInfo("保存成功");
                GridStore.Reload();
                PaperDetailAutoEditWindow.Hide();
            }
            else
            {
                MessageBoxHelper.ShowInfo("保存失败");
                return;

            }
        }

    }
}